<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="keywords" content="jquery,ui,easy,easyui,web">

    <title>发货管理</title>

    <link rel="stylesheet" type="text/css" href="<%=path %>/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="<%=path %>/themes/icon.css">
    <style type="text/css">
        #fm{
            margin:0;
            padding:10px 30px;
        }
        .ftitle{
            font-size:14px;
            font-weight:bold;
            color:#666;
            padding:5px 0;
            margin-bottom:10px;
            border-bottom:1px solid #ccc;
        }
        .fitem{
            margin-bottom:5px;
        }
        .fitem label{
            display:inline-block;
            width:80px;
        }
    </style>
    <script type="text/javascript" src="<%=path %>/js/jquery.min.js"></script>
    <script type="text/javascript" src="<%=path %>/js/jquery.easyui.min.js"></script>
    <script type="text/javascript">
        var url;
        function showError(msg) {
            $.messager.show({
                title: 'Error',
                msg: msg
            });
        }
        function newUser(){
            var rows = $('#dg').datagrid('getSelections');
            console.log(rows);
            var ids ="";
            var k = 0;
            for(var i=0 ;i < rows.length ; i++) {
                if (rows[i].state == '待出库') {
                    if(k==0){
                        ids+="\'"+rows[i].orderId+"\'";
                        k++;
                    }
                    else{
                        ids+=",\'"+rows[i].orderId+"\'";
                        k++;
                    }
                }
            }
            /*console.log(ids.substr(ids.length-1,1));
            console.log(ids.substr(0,ids.length-1));*/
            console.log(ids);
            if(ids == ""){
                showError("您所选择的订单都已经发货了");
            }else{
                $.ajax({
                    url: '/order/send',
                    type: 'post',
                    data: {'ids' : ids},
                    dataType: 'json',
                    timeout: 60000,
                    success: function (data) {
                        if(data.success == 'true') {
                            showError("发货成功");
                            $('#dg').datagrid('reload');
                        }
                        else {
                            showError("发货失败");
                        }
                    },
                    error: function () {

                        alert('网络异常');
                    }
                });
            }
        }

        function editUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                if(row.state=="待出库"){
                    $('#dlg2').dialog('open').dialog('setTitle','修改订单');
                    $('#fm2').form('load',row);
                    url = '<%=path %>/order/update';
                }else{
                    showError('只有待出库的才能修改地址');
                }
            }else {
                showError('请选中一条');
            }


        }
        function updateUser(){
            $('#fm2').form('submit',{
                url: url,
                onSubmit: function(){
                    return $(this).form('validate');
                },
                success: function(result){
                    var result = eval('('+result+')');
                    if (result.success){
                        $('#dlg2').dialog('close');		// close the dialog
                        $('#dg').datagrid('reload');	// reload the user data
                    } else {
                        showError(result.msg);
                    }
                }
            });
        }

        function removeUser(){
            var row = $('#dg').datagrid('getSelected');
            if (row){
                $.messager.confirm('Confirm','你确定要删除么?',function(r){
                    if (r){
                        $.post('/order/delete',{orderId : row.orderId},function(result){
                            if (result.success){
                                $('#dg').datagrid('reload');	// reload the user data
                            } else {
                                showError(result.msg);
                            }
                        },'json');
                    }
                });
            }else {
                showError('请选中一条');
            }
        }
    </script>

</head>

<body class="easyui-layout">

<table id="dg" title="发货列表" class="easyui-datagrid"
       url="<%=path %>/order/list" iconCls="icon-save" pagination="true"
       toolbar="#toolbar" rownumbers="true" fitColumns="true" singleSelect="false">
    <thead>

    <tr>
        <th field="orderId" width="50">订单id</th>
        <th field="state" width="50">订单状态</th>
        <th field="name" width="50">用户名</th>
        <th field="phone" width="50">用户电话</th>
        <th field="sumMoney" width="50">总价格</th>
        <th field="orderTime" width="50">下单时间</th>
        <th field="address" width="50">用户地址</th>
        <th field="goods" width="50">订单详情</th>
    </tr>
    </thead>

</table>

<div id="toolbar">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">发货</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">修改</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="removeUser()">取消</a>
</div>

<%--<div id="dlg" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
     closed="true" buttons="#dlg-buttons">
   &lt;%&ndash; <div class="">User Information</div>&ndash;%&gt;
        <div class="ftitle">
            <label>商品类别:</label>

            <select id="addselect">
                <option value="1" selected="selected">水果</option>
                <option value="2">手工品</option>
                <option value="3">服装</option>
                <option value="4">零食</option>
            </select>
        </div>
    <form id="fm" method="post" novalidate>
        <input name="goodstype" value="" type="hidden" id="addgoodstype"/>
        <div class="fitem">
            <label>商品名称:</label>
            <input name="goodsName">
        </div>
        <div class="fitem">
            <label>商品描述:</label>
            <textarea name="goodsContent" cols="22" rows="3" ></textarea>
        </div>
        <div class="fitem">
            <label>价格:</label>
            <input name="price" />
        </div>
        <div class="fitem">
            <label>库存:</label>
            <input name="stock" />
        </div>
        <div class="fitem">
            <label>图片url:</label>
            <input name="goodsImg" />
        </div>

    </form>
</div>--%>
<div id="dlg2" class="easyui-dialog" style="width:400px;height:280px;padding:10px 20px"
     closed="true" buttons="#dlg2-buttons">
    <%--<div class="ftitle">User Information</div>--%>
    <form id="fm2" method="post" novalidate>
        <input name="orderId" type="hidden">
        <div class="fitem">
            <label>用户地址:</label>
            <textarea name="address" value="" style="height: 150px;width: 200px"></textarea>
        </div>

    </form>
</div>
<%--<div id="dlg-buttons">
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">取消</a>
</div>--%>
<div id="dlg2-buttons">
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="updateUser()">修改</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#dlg2').dialog('close')">取消</a>
</div>
</body>
</html>

